# -*- coding: utf-8 -*-

from odoo import models, fields, api, modules
import chardet, random
import urllib, urllib2, sys, json, xlwt
import psycopg2
import time
import logging


class IrCron(models.Model):
    _inherit = 'ir.cron'
    _logger = logging.getLogger(__name__)

    @api.model
    def export_area_data(self):
        '''导出区域数据'''
        data = []
        provinces = self.env['mxbase.province'].search([])
        for province in provinces:
            if province.city_ids:
                for city in province.city_ids:
                    for county in city.county_ids:
                        data.append([province.id, province.name, city.id, city.name, county.id, county.name])
            else:
                for county in province.county_ids:
                    data.append([province.id, province.name, '', '', county.id, county.name])
        #写入excel
        path = '/Users/ljp/Desktop/area.xlsx'
        workbook = xlwt.Workbook(encoding='utf-8')
        worksheet = workbook.add_sheet('My Worksheet')
        for row in range(0, len(data)):
            for col in range(0, 6):
                val = data[row][col]
                worksheet.write(row, col, label=val)
        workbook.save(path)

    @api.model
    def mark_central_city(self):
        '''标识出省会城市 直辖市 特别行政区 首都'''
        capital = {'name': u'北京', 'code': u'11'}
        city_province = [
            {'name': u'北京', 'code': u'11'},
            {'name': u'天津', 'code': u'12'},
            {'name': u'上海', 'code': u'31'},
            {'name': u'重庆', 'code': u'50'},
        ]
        special_province = [
            {'name': u'香港', 'code': u'81'},
            {'name': u'澳门', 'code': u'82'},
        ]
        central_city = [
            {'name': u'河北', 'code': u'13', 'central_name': u'石家庄', 'central_code': u'1301'},
            {'name': u'河南', 'code': u'41', 'central_name': u'郑州', 'central_code': u'4101'},
            {'name': u'陕西', 'code': u'61', 'central_name': u'西安', 'central_code': u'6101'},
            {'name': u'山西', 'code': u'14', 'central_name': u'太原', 'central_code': u'1401'},
            {'name': u'山东', 'code': u'37', 'central_name': u'济南', 'central_code': u'3701'},
            {'name': u'甘肃', 'code': u'62', 'central_name': u'兰州', 'central_code': u'6201'},
            {'name': u'辽宁', 'code': u'21', 'central_name': u'沈阳', 'central_code': u'2101'},
            {'name': u'吉林', 'code': u'22', 'central_name': u'长春', 'central_code': u'2201'},
            {'name': u'黑龙江', 'code': u'23', 'central_name': u'哈尔滨', 'central_code': u'2301'},
            {'name': u'云南', 'code': u'53', 'central_name': u'昆明', 'central_code': u'5301'},
            {'name': u'贵州', 'code': u'52', 'central_name': u'贵阳', 'central_code': u'5201'},
            {'name': u'福建', 'code': u'35', 'central_name': u'福州', 'central_code': u'3501'},
            {'name': u'广东', 'code': u'44', 'central_name': u'广州', 'central_code': u'4401'},
            {'name': u'海南', 'code': u'46', 'central_name': u'海口', 'central_code': u'4601'},
            {'name': u'台湾', 'code': u'71', 'central_name': u'台北', 'central_code': u'7009'},
            {'name': u'四川', 'code': u'51', 'central_name': u'成都', 'central_code': u'5101'},
            {'name': u'湖北', 'code': u'42', 'central_name': u'武汉', 'central_code': u'4201'},
            {'name': u'湖南', 'code': u'43', 'central_name': u'长沙', 'central_code': u'4301'},
            {'name': u'江西', 'code': u'36', 'central_name': u'南昌', 'central_code': u'3601'},
            {'name': u'安徽', 'code': u'34', 'central_name': u'合肥', 'central_code': u'3401'},
            {'name': u'江苏', 'code': u'32', 'central_name': u'南京', 'central_code': u'3201'},
            {'name': u'浙江', 'code': u'33', 'central_name': u'杭州', 'central_code': u'3301'},
            {'name': u'青海', 'code': u'63', 'central_name': u'西宁', 'central_code': u'6301'},
            {'name': u'新疆维吾尔自治区', 'code': u'65', 'central_name': u'乌鲁木齐', 'central_code': u'6501'},
            {'name': u'内蒙古自治区', 'code': u'15', 'central_name': u'呼和浩特', 'central_code': u'1501'},
            {'name': u'宁夏回族自治区', 'code': u'64', 'central_name': u'银川', 'central_code': u'6401'},
            {'name': u'西藏自治区', 'code': u'54', 'central_name': u'拉萨', 'central_code': u'5401'},
            {'name': u'广西壮族自治区', 'code': u'45', 'central_name': u'南宁', 'central_code': u'4501'},
        ]
        province_obj = self.env['mxbase.province']
        city_obj = self.env['mxbase.city']
        for item in central_city:
            province = province_obj.search([('code', '=', item.get('code'))])
            if province:
                city = city_obj.search([('code', '=', item.get('central_code'))])
                province.central_city_id = city
        for item in special_province:
            province = province_obj.search([('code', '=', item.get('code'))])
            if province:
                province.special_province = True
        for item in city_province:
            province = province_obj.search([('code', '=', item.get('code'))])
            if province:
                province.city_province = True
        province = province_obj.search([('code', '=', capital.get('code'))])
        if province:
            province.capital = True

    @api.model
    def import_area(self):
        '''导入行政区域'''
        province_obj = self.env['mxbase.province']
        city_obj = self.env['mxbase.city']
        county_obj = self.env['mxbase.county']
        with open(modules.get_module_resource('mxnet_base', 'data', 'area.txt'), 'r') as f:
            content = f.read()
        data = eval(content)
        for item in data:
            province_name = item.get('name')
            code = item.get('id')
            province = province_obj.search([('code', '=', code)])
            if province:
                province.name = province_name
            else:
                province = province_obj.create({
                    'name': province_name,
                    'code': code,
                })
            city_items = item.get('city')
            if city_items:
                for city_item in city_items:
                    name = city_item.get('name')
                    code = city_item.get('id')
                    city = city_obj.search([('code', '=', code)])
                    if city:
                        city.write({
                            'name': name,
                            'code': code,
                            'province_id': province.id,
                        })
                    else:
                        city = city_obj.create({
                            'name': name,
                            'code': code,
                            'province_id': province.id,
                        })
                    county_items = city_item.get('county')
                    for county_item in county_items:
                        name = county_item.get('name')
                        code = county_item.get('id')
                        county = county_obj.search([('code', '=', code)])
                        if county:
                            county.write({
                                'name': name,
                                'code': code,
                                'province_id': province.id,
                                'city_id': city.id,
                            })
                        else:
                            county = county_obj.create({
                                'name': name,
                                'code': code,
                                'province_id': province.id,
                                'city_id': city.id,
                            })
            elif item.get('county'):
                county_items = item.get('county')
                for county_item in county_items:
                    name = county_item.get('name')
                    code = county_item.get('id')
                    county = county_obj.search([('code', '=', code)])
                    if county:
                        county.write({
                            'name': name,
                            'code': code,
                            'province_id': province.id,
                            'city_id': False,
                        })
                    else:
                        county = county_obj.create({
                            'name': name,
                            'code': code,
                            'province_id': province.id,
                            'city_id': False,
                        })
